package com.eva.admin.api.system;

import com.eva.framework.common.api.BaseController;
import com.eva.admin.biz.system.SystemLocationBiz;
import com.eva.admin.biz.system.dto.UpdateSystemLocationDTO;
import com.eva.admin.biz.system.dto.CreateSystemLocationDTO;
import com.eva.dao.system.model.SystemLocation;
import com.eva.admin.dao.system.dto.QuerySystemLocationDTO;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.rbac.trace.Trace;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "地区管理")
@RestController
@RequestMapping("/system/location")
public class SystemLocationController extends BaseController {

    @Resource
    private SystemLocationBiz systemLocationBiz;

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("system:location:create")
    public ApiResponse<?> create(@RequestBody CreateSystemLocationDTO dto) {
        systemLocationBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    @ContainPermissions("system:location:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        systemLocationBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @ContainPermissions("system:location:delete")
    public ApiResponse<?> deleteByIdInBatch(@RequestParam String ids) {
        systemLocationBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("修改")
    @PostMapping("/updateById")
    @ContainPermissions("system:location:update")
    public ApiResponse<?> updateById(@RequestBody UpdateSystemLocationDTO dto) {
        systemLocationBiz.updateById(dto);
        return ApiResponse.success(null);
    }
    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("system:location:query")
    public ApiResponse<PageData<SystemLocation>> findPage (@RequestBody PageWrap<QuerySystemLocationDTO> pageWrap) {
        return ApiResponse.success(systemLocationBiz.findPage(pageWrap));
    }

    @Trace(exclude = true)
    @ApiOperation("查询子地区")
    @GetMapping("/children/{parentId}")
    public ApiResponse<List<SystemLocation>> findChildren (@PathVariable Integer parentId) {
        return ApiResponse.success(systemLocationBiz.findChildren(parentId));
    }
}
